
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

int main()
{
	key_t key;
	struct msgbuf {
		long tp;
		char mm[100];
	} mybuf;
	int len, msgid, maxlen = 100;
	if((key = ftok("som.dat", 0)) < 0) {
		cout << "can't create key" << endl;
		exit(-1);
	}
	if((msgid = msgget(key, IPC_CREAT|0644)) < 0) {
		cout << "something is incorrect" << endl;
		exit(-1);
	}
	while(1) {
		if((len = msgrcv(msgid, &mybuf, maxlen, 0, 0)) < 0) {
			exit(-1);
		}
		if(mybuf.tp == 300) {
			cout << "end session" << endl;
			msgctl(msgid, 0, 0);
			exit(-1);
		}
		write(1, mybuf.mm, len);
		if(!strcmp(mybuf.mm, "exit\n")) {
			cout << "end session !!"<< endl;
			msgctl(msgid, 0, 0);	
			break;
		}
	} 
	return 0;
}
